package com.ujiuye.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String timeStr) {
        //timeStr前台过来的字符串 2020/11/19    2020-11-19      22:53:25
        String pattern = "yyyy/MM/dd";
        if (timeStr.contains("-")){
            pattern = "yyyy-MM-dd";
        }
        if (timeStr.contains(":")){
            pattern += " HH:mm:ss";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
